
import { EquipmentService } from './../services/equipment';
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Globals } from './../../../shared/global';

@Component({
  selector: 'ree-bind-device',
  templateUrl: './bind-device.component.html',
  styleUrls: ['./bind-device.component.scss']
})
export class BindDeviceComponent implements OnInit {

  _allChecked = false;
  _indeterminate = false;

  devicesList=[];

  constructor(
    private global:Globals,
    private equipmentService:EquipmentService,
    private router:Router,
    private route:ActivatedRoute
  ) { }

  ngOnInit() {
    this.route.params.subscribe(
      params => console.log(params['id'])
    );
    this.getdevicesList();
  }

  getdevicesList() {
    this.equipmentService.getdevicesList(1).then(data => this.devicesList = data)
  }

  confirmBind(){
    this.router.navigateByUrl("work/equipment/list")
  }

  _refreshStatus() {
    const allChecked = this.devicesList.every(value => value.checked === true);
    const allUnChecked = this.devicesList.every(value => !value.checked);
    this._allChecked = allChecked;
    this._indeterminate = (!allChecked) && (!allUnChecked);
    console.log(allChecked,allUnChecked,this._allChecked)
  };

  _checkAll(value) {
    console.log(value)
    if (value) {
      this.devicesList.forEach(data => {
        data.checked = true;
      });
    } else {
      this.devicesList.forEach(data => {
        data.checked = false;
      });
    }
    this._refreshStatus();
  };

  ngOnDestroy(){
    this.global.paths.path.pop()
    this.global.paths.path.pop()
  }

}
